package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.ExplorationService;
import com.dp.appi.service.ProjectStatisticsService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Created by bchen on 2016/5/23.
 * 项目统计
 */
@Controller
@RequestMapping(value = "/projectStatisticsControl")
public class ProjectStatisticsControl {
    private static Logger log = LoggerFactory.getLogger(ProjectStatisticsControl.class);

    @Autowired
    private ProjectStatisticsService projectStatisticsService;

    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private ExplorationService explorationService;

    /**
     *  个人项目进度统计
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/scheduleCount",method = RequestMethod.GET)
    public void scheduleCount(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            String username = userEntity.getA();
            List<Map<String, Object>> count = projectStatisticsService.scheduleCount(username);
            modelMap.addAttribute("ongoing", 0);
            modelMap.addAttribute("completed", 0);
            for (int i = 0; null != count && i < count.size(); i++) {
                Map<String, Object> m = count.get(i);
                Object finish = m.get("finish");
                if (finish != null && "0".equals(String.valueOf(finish))) {
                    modelMap.addAttribute("ongoing", m.get("num"));
                }
                if (finish != null && "1".equals(String.valueOf(finish))) {
                    modelMap.addAttribute("completed", m.get("num"));
                }
            }
        } catch (CommonException c) {
            modelMap.remove("ongoing");
            modelMap.remove("completed");
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.remove("ongoing");
            modelMap.remove("completed");
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     *  阶段线路杆塔明细
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/poleSum",method = RequestMethod.GET)
    public void poleSum(ModelMap modelMap, @RequestParam Map<String,String> param) {
        try {
            String voltageGrade = param.get("voltageGrade");
            String stage_id = param.get("stage_id");
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            List<Map<String, Object>> res = projectStatisticsService.poleSum(voltageGrade,Long.valueOf(stage_id),String.valueOf(map.get("id")));
            modelMap.addAttribute("poleSum",res);
        }catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch(Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 阶段台区材料统计
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/materialSum", method = RequestMethod.GET)
    public void materialSum(ModelMap modelMap, @RequestParam Map<String, String> param, Long explor_id) {
        try {
            Long stage_id = Long.valueOf(param.get("stage_id"));
            String exploration_type = param.get("exploration_type");

            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Integer classify = explorationService.getClassify(stage_id, String.valueOf(map.get("id")));

            modelMap.addAttribute("materialSum", projectStatisticsService.materialSum(stage_id,classify,explor_id,exploration_type));

        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 阶段用电户
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/userPoint", method = RequestMethod.GET)
    public void userPoint(ModelMap modelMap, @RequestParam Map<String, String> param, Long stage_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            modelMap.addAttribute("userPoint", projectStatisticsService.userPoint(stage_id,String.valueOf(map.get("id"))));
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 项目设计阶段统计
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/projectSum", method = RequestMethod.GET)
    public void projectSum(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Map<String, Object> ma = consumerService.getUserByUsername(String.valueOf(map.get("username")));

            modelMap.addAllAttributes(projectStatisticsService.projectSum(param,String.valueOf(map.get("id")),ma.get("org_id")));
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 项目按字段统计
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/fieldSum", method = RequestMethod.GET)
    public void fieldSum(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Map<String, Object> ma = consumerService.getUserByUsername(String.valueOf(map.get("username")));
            modelMap.addAttribute("fieldSum",projectStatisticsService.fieldSum(param,String.valueOf(map.get("id")),ma.get("org_id")));
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }

    }

    /**
     * 阶段交叉跨越
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/sectionCorss", method = RequestMethod.GET)
    public void sectionCorss(ModelMap modelMap, @RequestParam Map<String, String> param,Long explor_id) {
        try {
            Long stage_id = Long.valueOf(param.get("stage_id"));
            String exploration_type = param.get("exploration_type");

            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Integer classify = explorationService.getClassify(stage_id, String.valueOf(map.get("id")));

            modelMap.addAttribute("describe",projectStatisticsService.describe(stage_id,classify,explor_id,exploration_type));
            modelMap.addAttribute("list",projectStatisticsService.corssList(stage_id,classify,explor_id,exploration_type));
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 项目阶段点位统计
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/pointSum", method = RequestMethod.GET)
    public void pointSum(ModelMap modelMap, @RequestParam Map<String, String> param,Long stage_id,Long explor_id) {
        try {
            //TODO 获取数据类别
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            modelMap.addAllAttributes(projectStatisticsService.pointSum(stage_id,String.valueOf(map.get("id")),explor_id));
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 项目阶段台区统计
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/explorSum", method = RequestMethod.GET)
    public void explorSum(ModelMap modelMap, @RequestParam Map<String, String> param,Long explor_id) {
        try {
            modelMap.addAllAttributes(projectStatisticsService.explorSum(explor_id));
        }  catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }  catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }
}
